package com.baosight.sczx.common.zm.basetools.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkSchedule;

/**
 * 
 * <p>Title: GenerateDutySchedule.java</p>
 * <p>Description: 生成调度值班情况</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author bobswang
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-2 下午03:44:47  bobswang  Create
 *</pre>
 */
public class GenerateDutySchedule {
	private Dao dao;
	
	public GenerateDutySchedule(){
	}
	
	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	
	/**
	 * 生成调度值班记录
	 * <p>Function: GenerateDuty</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-5上午09:11:38 严洪浩
	 * </p>
	 *
	 */
	public void GenerateDuty() throws Exception {
		String currentDate = StaticTools.getDate();
		String date = StaticTools.getTomorrow("");
		String vitualDate = date;
		//如果时间等于20点，才执行
		int hour = Integer.parseInt(StaticTools.getTime().substring(0, 2));
		if(hour != 20)
		{
			return;
		}
		
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdDate", date);
		
		List<?>  schedule_result = dao.query("ZMCPDutySchedule.query", _argMap);
		//如果已经生成过了，则不执行
		if(schedule_result.size() != 0)
		{
			return;
		}
		
		List<?> _result = dao.query("ZMCSWorkSchedule.query", new HashMap<String, String>());
		
		for(int i=0; i<_result.size(); i++)
		{
			String keyId = TablePk.getInstance().getNextValue("T_CP_DUTYSCHEDULE");
			
			ZMCSWorkSchedule workSchedule = (ZMCSWorkSchedule)_result.get(i);
			String fdWorkNumId = workSchedule.getFdKeyId();
			
			Map<String, String> _insertMap = new HashMap<String, String>();
			_insertMap.put("fdKeyId", keyId);
			_insertMap.put("fdVirtualDate", vitualDate);
			_insertMap.put("fdCurrentDate", currentDate);
			_insertMap.put("fdDate", date);
			_insertMap.put("fdWorkNumId", fdWorkNumId);
			
			dao.insert("ZMCPDutySchedule.insert", _insertMap);
		}
	}
}
